import axios from 'axios'
import store from '@/store'
import request from '@/utils/request'
import router from '@/router'
import { getToken } from '@/utils/auth'

const Sync = {
    source: null,
    isDisconnect:true,
    connect(){
        this.isDisconnect = false;
        if (getToken()) {
            this.sync()
        }
    },
    sync() {
        if(this.isDisconnect)
            return;
        const CancelToken = axios.CancelToken;
        this.source = CancelToken.source();
        // console.log('this.source.token',this.source.token)
        request
        .post("api/sync", {}, {
            cancelToken: this.source.token,
            timeout: 35000,
            params: {
                format: "sync"
            }
        })
        .then(events => {
            // alert('events')
            events.forEach(msg => {
                const data = msg.data;
                try {
                    if(!data){
                        this.invokes[msg.biz][msg.action]();
                    }else{
                        console.log(msg)
                        this.invokes[msg.biz][msg.action](data);
                    }
                } catch (e) {
                    console.error('error',e);
                }
            });
        })
        .catch(err => {
            // alert('err')
            let message = err.message
            console.log('sync请求错误:' + err.message)
            if (message.indexOf('404') != -1) {
                this.disconnect()
            }
        })
        .finally(() => {
            this.sync();
        });
    },
    // 切断请求
    disconnect(){
        this.isDisconnect = true;
        this.cancel();
    },
    cancel(){
        if(this.source)
            this.source.cancel();
    },
    invokes:{
        SYS:{
            //后台修改配置通知
            CONFIG(data){
                // store.dispatch("sys/getSysConfig").then(data => {
                //     console.log(data.info)
                // });
                store.commit("sys/SET_CONFIG_SINGLE", data);
            }
        },
        USER:{
            // 用户信息变更
            UPDATE(){
                // alert('UPDATE')
                store.dispatch("user/getUserInfo").then(data => {
                    console.log('用户信息',data.info)
                });
            }
        },
        MESSAGE:{
            
        },
        ORDER:{
            NEW(data) {
                console.log(`有新订单${data}`)
            },
            PAY(data) {
                console.log(`订单${data}已支付`)
                let path = router.app._route.fullPath
                if (path.indexOf('/order') != -1) {
                    let active = router.app._route.query.tabId
                    console.log(active)
                    store.dispatch('order/getOrderList', {
                        active,
                        eventName: 'click',
                        pageNum: 1
                    })
                }
                // 在其它页面支付要跳转
                router.push({ 
                    name: 'order',
                    query: {
                        tabId: 0
                    }
                })
            },
            SHIP(data) {
                console.log(`订单${data}已发货`)
                let path = router.app._route.fullPath
                if (path.indexOf('/order') != -1) {
                    let active = router.app._route.query.tabId
                    store.dispatch('order/getOrderList', {
                        active,
                        eventName: 'click',
                        pageNum: 1
                    })
                }
            },
            RECEIVE(data) {
                console.log(`订单${data}已确认收货`)
            },
            CANCEL(data) {
                console.log(`订单${data}已取消`)
            },
            CLOSE(data) {
                console.log(`订单${data}已关闭`)
            },
            FINISH(data) {
                console.log(`订单${data}已完成`)
            },
            RECEIVE_INFO(data) {
                console.log(`订单${data}收货地址修改`)
            },
            AMOUNT_INFO(data) {
                console.log(`订单${data}金额变更`)
            },
            NOTE_INFO (data) {
                console.log(`订单${data}备注修改`)
            }
        },
        // 售后
        RETURNAPPLY:{
            NEW(data){
                console.log(`订单${data}发起售后`)
            },
            REFUSE(data){
                console.log(`订单${data}售后已拒绝`)
            },
            ACCEPT(data){
                console.log(`订单${data}售后已同意`)
            },
            SHIP(data){
                console.log(`订单${data}已退货`)
            }
        },
        PRODUCT:{
            UPDATE(data){
                let {type}=data
                // console.log('data',data)
                let hotProductList = store.state.home.content.hotProductList.info;
                if(type == 0){
                    // console.log('advertiseList[i]',store.state.home.content)
                    for(let i = 0;i<hotProductList.length;i++){
                        // console.log('advertiseList[i]',hotProductList[i].id)
                        if(data.ids.indexOf(hotProductList[i].id) != -1){
                            hotProductList.splice(i,1);
                        }
                    }
                    store.commit('home/SET_HOT_LIST_INFO',hotProductList)
                }
                // else if(type == 2){
                //     for(let i = 0;i<hotProductList.length;i++){
                //         if(data.ids.indexOf(hotProductList[i].id) != -1){
                //             hotProductList.splice(i,1);
                //         }
                //     }
                //     store.commit('home/SET_HOT_LIST_INFO',hotProductList)
                // }
                // console.log(123456,data)
            }
        },
        ADVERTISE: {
            UPDATE() {
                try {
                    store.dispatch("home/getSdvertiseList").then((data) => {
                    });
                } catch (e) {
                }
            }
        }
    }
}

export default Sync